package com.happy3w.ssp.schoolmatehome.controller;

import com.happy3w.ssp.schoolmatehome.entity.User;
import com.happy3w.ssp.schoolmatehome.interceptor.AttributeKeys;
import com.happy3w.ssp.schoolmatehome.service.auth.TheUserDetail;
import com.happy3w.ssp.schoolmatehome.service.note.NoteSearcher;
import com.happy3w.ssp.schoolmatehome.service.note.NoteSummary;
import com.happy3w.ssp.schoolmatehome.service.user.UserInfo;
import com.happy3w.ssp.schoolmatehome.service.user.UserSearcher;
import com.happy3w.ssp.schoolmatehome.service.user.UserService;
import com.happy3w.ssp.schoolmatehome.service.user.UserSummary;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/me")
@PreAuthorize("hasRole('ROLE_NAMED')")
public class MeController {
    private final UserService userService;
    private final NoteSearcher noteSearcher;
    private final UserSearcher userSearcher;

    @GetMapping()
    public UserInfo getUserInfo(@RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail) {
        return UserInfo.from(userDetail.getUser());
    }

    @PutMapping("name")
    public UserInfo updateUserName(@RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail,
                                   @RequestBody UpdateUserNameReq req) {
        User user = userService.updateName(userDetail.getUser().getId(), req.name);
        userDetail.getUser().setName(user.getName());
        return UserInfo.from(user);
    }

    @GetMapping("note-history")
    public List<NoteSummary> findAllNoteHistory(
            @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail,
            @RequestParam(required = false, name = "query") String query,
            @RequestParam(required = false, name = "page", defaultValue = "0") int page,
            @RequestParam(required = false, name = "size", defaultValue = "20") int size) {
        return noteSearcher.findAllNoteHistory(userDetail.getUser(), query, page, size);
    }

    @GetMapping("note-favorite")
    public List<NoteSummary> findAllNoteFavorite(
            @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail,
            @RequestParam(required = false, name = "query") String query,
            @RequestParam(required = false, name = "page", defaultValue = "0") int page,
            @RequestParam(required = false, name = "size", defaultValue = "20") int size) {
        return noteSearcher.findAllNoteFavorite(userDetail.getUser(), query, page, size);
    }

    @GetMapping("note-collect")
    public List<NoteSummary> findAllNoteCollect(
            @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail,
            @RequestParam(required = false, name = "query") String query,
            @RequestParam(required = false, name = "page", defaultValue = "0") int page,
            @RequestParam(required = false, name = "size", defaultValue = "20") int size) {
        return noteSearcher.findAllNoteCollect(userDetail.getUser(), query, page, size);
    }

    @GetMapping("note-template")
    public List<NoteSummary> findAllNoteTemplate(
            @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail,
            @RequestParam(required = false, name = "query") String query,
            @RequestParam(required = false, name = "page", defaultValue = "0") int page,
            @RequestParam(required = false, name = "size", defaultValue = "20") int size) {
        return noteSearcher.findAllNoteTemplate(userDetail.getUser(), query, page, size);
    }

    @Data
    public static class UpdateUserNameReq {
        private String name;
    }

    @GetMapping("idols")
    public List<UserSummary> findIdols(
            @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail,
            @RequestParam(required = false, name = "query") String query,
            @RequestParam(required = false, name = "page", defaultValue = "0") int page,
            @RequestParam(required = false, name = "size", defaultValue = "20") int size) {
        return userSearcher.findIdols(userDetail.getUser(), query, page, size);
    }

    @GetMapping("fans")
    public List<UserSummary> findFans(
            @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail,
            @RequestParam(required = false, name = "query") String query,
            @RequestParam(required = false, name = "page", defaultValue = "0") int page,
            @RequestParam(required = false, name = "size", defaultValue = "20") int size) {
        return userSearcher.findFans(userDetail.getUser(), query, page, size);
    }
}
